package cn.tinlan.shopcoin.app.mine.order;

import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import com.hjq.bar.OnTitleBarListener;
import com.hjq.bar.TitleBar;
import com.zhouyou.http.EasyHttp;
import com.zhouyou.http.callback.CallBackProxy;
import com.zhouyou.http.callback.SimpleCallBack;
import com.zhouyou.http.exception.ApiException;
import com.zhouyou.http.model.HttpParams;

import butterknife.BindView;
import cn.tinlan.shopcoin.R;
import cn.tinlan.shopcoin.Utils.SPUtils;
import cn.tinlan.shopcoin.Utils.ToastUtils;
import cn.tinlan.shopcoin.app.mine.adress.AddressListActivity;
import cn.tinlan.shopcoin.app.mine.adress.model.AddressItemModel;
import cn.tinlan.shopcoin.base.BaseActivity;
import cn.tinlan.shopcoin.base.CustomApiResult;
import cn.tinlan.shopcoin.widget.category.Constants;

/**
 * Created by Mortimer on 2018/12/12. 10:09
 * 佛祖🙏，永无bug。
 */
public class OrderDetailActivity extends BaseActivity implements View.OnClickListener{


    @BindView(R.id.order_detail_title)
    TitleBar orderDetailTitleBar;
    @BindView(R.id.order_detail_address)
    View orderDetailAddress;
    @BindView(R.id.order_detail_name)
    TextView order_detail_name;
    @BindView(R.id.order_detail_mobile)
    TextView order_detail_mobile;
    @BindView(R.id.detail_address_content)
    TextView detail_address_content;


    private String addressID = "1";
    private String goods_id;
    private String stock_id;
    private String buy_num;

    private String orderID;
    @Override
    protected int provideContentViewId() {
        return R.layout.activity_order_detail;
    }


    @Override
    public void initView() {
        super.initView();



        initOrderDetailTitleBar();
        initOrderDetailAddress();
    }


    @Override
    public void initData() {
        super.initData();
        loadDefaultAddress();
    }

    private void initOrderDetailTitleBar() {
        orderDetailTitleBar.setOnTitleBarListener(new OnTitleBarListener() {
            @Override
            public void onLeftClick(View v) {
                finish();
            }

            @Override
            public void onTitleClick(View v) {

            }

            @Override
            public void onRightClick(View v) {

            }
        });
    }

    private void initOrderDetailAddress() {

        orderDetailAddress.setOnClickListener(this);
    }

    //弹出支付菜单
    private void presentPayView() {
        new PayDialog(OrderDetailActivity.this).setData(20,30)
                .setListener(new PayDialog.OnPayClickListener() {
                    @Override
                    public void onPayClick(int payType) {
                        switch (payType) {
                            case PayDialog.ALI_PAY:
                                ToastUtils.show("支付宝");

                                break;
                            case PayDialog.WX_PAY:
                                ToastUtils.show("微信");

                                break;
                            case PayDialog.BALANCE_PAY:
                                ToastUtils.show("余额");

                                break;
                        }
                    }
                }).show();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.order_detail_address:
                startActivity(new Intent(OrderDetailActivity.this, AddressListActivity.class));
                break;
        }
    }

    private void loadDefaultAddress() {
        EasyHttp.get(Constants.UserDefaultAddress)
                .params("user_id", SPUtils.getUserID(OrderDetailActivity.this))
                .execute(new CallBackProxy<CustomApiResult<AddressItemModel>, AddressItemModel>(new SimpleCallBack<AddressItemModel>() {
                    @Override
                    public void onError(ApiException e) {
                        //请求错误
                    }

                    @Override
                    public void onSuccess(AddressItemModel response) {
                        AddressItemModel itemModel = response;
                        order_detail_name.setText(itemModel.getName());
                        order_detail_mobile.setText(itemModel.getTel());
                        order_detail_mobile.setText(itemModel.getZone());
                    }

                }) {
                });
    }


    private void createOrder() {

        HttpParams params = new HttpParams();
        params.put("user_id",SPUtils.getUserID(OrderDetailActivity.this));
        params.put("address_id",addressID);

        StringBuilder cartList = new StringBuilder();
        for (int i = 0; i<5;i++) {

            cartList.append("{"+goods_id+","+stock_id+","+buy_num+"}");
        }
        params.put("cart","["+cartList.toString()+"]");
        params.put("remark",addressID);
        params.put("mtype",addressID);

        EasyHttp.post(Constants.OrderCreate)
                .params(params)
                .execute(new CallBackProxy<CustomApiResult<String>, String>(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {
                        //请求错误
                    }

                    @Override
                    public void onSuccess(String response) {
//                        ToastUtils.show("");

                    }

                }) {
                });
    }


    private void  deleteOrder() {
        EasyHttp.get(Constants.OrderDelete)
                .params("id",orderID)
                .execute(new CallBackProxy<CustomApiResult<String>, String>(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {
                        //请求错误
                    }

                    @Override
                    public void onSuccess(String response) {
//                        ToastUtils.show("");

                    }

                }) {
                });

    }


    private void  OrderPay() {
        EasyHttp.get(Constants.OrderPay)
                .params("id",orderID)
                .execute(new CallBackProxy<CustomApiResult<String>, String>(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {
                        //请求错误
                    }

                    @Override
                    public void onSuccess(String response) {
//                        ToastUtils.show("");

                    }

                }) {
                });

    }

        //确认收货
    private void  OrderConfirm() {
        EasyHttp.get(Constants.OrderConfirm)
                .params("id",orderID)
                .execute(new CallBackProxy<CustomApiResult<String>, String>(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {
                        //请求错误
                    }

                    @Override
                    public void onSuccess(String response) {
//                        ToastUtils.show("");

                    }

                }) {
                });

    }
}
